      $(function(){
          //获取所有未成交的app订单，同时获取所有商家的信息
          $("input[name=type]").click(function(){
              var type = $(this).val();
              toggleStuSoc(type);
          });
      
          //商家编号与商家名称的下拉框切换
          $(".shopNum").change(function(){
             var op = $(this).children('option:selected');
             var s = $("select[name=shopName]");
             s.val(s.find("option[id="+op.attr("id")+"]").val());
             $("input[name=shopId]").val(op.attr("id"));
          });
          
          $(".shopName").change(function(){
             var op = $(this).children('option:selected');
             var s = $("select[name=shopNum]");
             s.val(s.find("option[id="+op.attr("id")+"]").val());
             $("input[name=shopId]").val(op.attr("id"));
          });
          $("input[name=contractId]").focusin(function(){
        	  if($(this).val()=='新合同请勿填写'){
        		  $(this).val("");
        	  }
          });
          
          $("input[name=borrowTime]").focusin(function(){
        	  if($(this).val()=='新合同请勿填写'){
        		  $(this).val("");
        	  }
          });
          
          $("input[name=paidPeriodT]").focusin(function(){
        	  if($(this).val()=='新合同请勿填写'){
        		  $(this).val("");
        	  }
          });
          
          $("input[name=paidPeriodT]").focusout(function(){
        	  if($(this).val()=='新合同请勿填写' || isNaN($(this).val())==true){
        		  $(this).val("");
        	  }
          });
          
          $("input[name=commodityPrice]").focusout(function(){
        	 var borrowM = $("input[name=borrowMoney]").val();
        	 var commodityPrice = $("input[name=commodityPrice]").val();
        	 if(commodityPrice!='' && borrowM!=''){
        		 $("input[name=paidAlready]").val(commodityPrice - borrowM);
        	 }
          });
          
          $("input[name=borrowMoney]").focusout(function(){
        	  var borrowM = $("input[name=borrowMoney]").val();
         	 var commodityPrice = $("input[name=commodityPrice]").val();
         	 if(commodityPrice!='' && borrowM!=''){
         		 $("input[name=paidAlready]").val(commodityPrice - borrowM);
         	 }
          });
          
      });
      
      //获取商家信息的回调函数
      var selectS = function(text){
          var obj = eval('(' + text + ')');
          if(obj.code != 1){
              alert("获取失败");
              return ;
          }
          $("select[name=shopNum]").empty();
          $("select[name=shopName]").empty();
          for(var i = 0 ; i < obj.data.length; i++){
              var d = obj.data[i];
               var op1 = $("<option></option>");
               op1.text(d.number);
               op1.val(d.number);
               op1.attr("id",d.id);
               $("select[name=shopNum]").append(op1);
               var op2 = $("<option></option>");
               op2.text(d.name);
               op2.val(d.name);
               op2.attr("id",d.id);
               $("select[name=shopName]").append(op2);
          }
          $("input[name=shopId]").val($("select[name=shopNum] option:selected").attr("id"));
      };
      
      //删除一行联系人
      function deleteContact(index){
           if(confirm("确定要删除么")){
                var row =  $("#contactT").find("tr.data").eq(index);
                var id = row.find("input[name=idC]").val();
                if(id != ""){
                request(path+"/deleteContact.do","id="+id,function(text){
                     var obj = eval('(' + text + ')');
                       if(obj.code != 1){
                          alert("删除失败");
                          return ;
                       }else{
                          alert("删除成功");
                       }
                 });
               }
               row.remove();
           }
      }
      var contactIndex;
      //保存联系人
      function saveContact(index){
         contactIndex = index;
         var row = $("#contactT").find("tr.data").eq(index);
         var id = row.find("input[name=idC]").val();
         var name = row.find("input[name=nameC]").val();
         var relation = row.find("select[name=relationC]").eq(0).val();
         var phone = row.find("input[name=phoneT]").val();
         var userid = $("input[name=userid]").val();
         if(userid == ""){
             alert("请先保存用户信息");
             return false;
         }
         if(name == "" || relation == "" || phone == ""){
              alert("请填写完整信息");
             return false;
         }
         var data = 'id='+id+'&userid='+userid+'&name='+name+'&relation='+relation+'&phone='+phone;
         request(path+"/addContact.do",data,contactF);
      }
      //添加一行联系人
      function addContact(){
          var rows = $("#contactT tr.data").length;
          var html = '<tr class="data"><td class="hide"><input name="idC" type="text" value=""/></td><td width="10%">姓名</td><td width="20%"><input class="input" type="text" name="nameC"/></td><td width="10%">关系</td><td width="15%"><select name="relationC"><option value="父亲">父亲</option><option value="母亲">母亲</option><option value="兄弟">兄弟</option><option value="姐妹">姐妹</option><option value="夫妻">夫妻</option><option value="同事">同事</option><option value="朋友">朋友</option><option value="同学">同学</option></select></td><td width="10%">手机号码</td><td width="20%"><input class="input" type="text" name="phoneT"/></td><td><a onclick="saveContact(' + rows +')" href="javascript:void(0)">保存</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a onclick="deleteContact('+ rows +')" href="javascript:void(0)">删除</a></td></tr>';
          $(html).insertBefore("#contactT tr:last");
      }
      
      //保存用户信息
      function saveUser(){
        var re = formValidate("userF");
        if(re==false){
            return;
        }
	     var query = $("#userF").formSerialize();
	     var type = $("input[name=type]:checked").val();
	     var userid = $("input[name=userid]").val();
	     query = query + "&id="+userid+"&type="+type;
	     request(path+"/addUser.do",query,function(text){
	         var obj = eval('(' + text + ')');
	         if(obj.code != 1){
              alert("保存失败");
              return ;
             }else{
                 alert("保存成功");
                 $("input[name=userid]").val(obj.data);
             }
	     });
	   }
	   
	   function importUser(){
	       var pattern =/\w{15}(\w{3})?/;
	       var cardId=$("input[name=cardId]").val();
	       if(pattern.test(cardId)==true){
	          getUser("",cardId);
	       }else{
	          alert("请输入正确的身份证号格式");
	       }
	       
	   }
	   
	   //根据id或者cardId拉取用户信息
	   function getUser(id,cardId){
	        request(path+"/getUser.do","userid="+id+"&cardId="+cardId,function(text){
	         var obj = eval('(' + text + ')');
	         if(obj.code != 1){
              alert("对应用户不存在");
              return ;
             }else{
                setUserData(obj.data);
             }
	       });
	   }
	   
	   //设置用户信息，包括图片信息
	   function setUserData(data){
	       var user =data['user'];
	       var contacts = data['contacts'];
	       var type=user['type'];
	       toggleStuSoc(type);
	       $("input[name=type][value="+type+"]").attr("checked","true");
	       $("#userF input[type!=button]").each(function(){
	           var name = $(this).attr('name');
	           if($(this).attr("type")=='radio'){
	               $("input[name="+name+"][value="+user[name]+"]").attr("checked","true");
	           }else{
	               $(this).val(user[name]);
	           }
	       });
	       $("#userF select").each(function(){
	           var name = $(this).attr('name');
	           $(this).val(user[name]);
	       });
	       //设置联系人id
	       $("input[name=userid]").val(user['id']);
	       //设置联系人信息
	       $("#contactT tr.data").remove();
	       for(var i=0; i< contacts.length; i++){
	           var con = contacts[i];
	           var html = $('<tr class="data"><td class="hide"><input name="idC" type="text" value=""/></td><td width="10%">姓名</td><td width="20%"><input class="input" type="text" name="nameC"/></td><td width="10%">关系</td><td width="15%"><select name="relationC"><option value="父亲">父亲</option><option value="母亲">母亲</option><option value="兄弟">兄弟</option><option value="姐妹">姐妹</option><option value="夫妻">夫妻</option><option value="同事">同事</option><option value="朋友">朋友</option><option value="同学">同学</option></select></td><td width="10%">手机号码</td><td width="20%"><input class="input" type="text" name="phoneT"/></td><td><a onclick="saveContact(' + i +')" href="javascript:void(0)">保存</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a onclick="deleteContact('+ i +')" href="javascript:void(0)">删除</a></td></tr>');
               html.find("input[name=idC]").val(con['id']);
               html.find("input[name=nameC]").val(con['name']);
               html.find("select[name=relationC]").val(con['relation']);
               html.find("input[name=phoneT]").val(con['phone']);
               html.insertBefore("#contactT tr:last");
	       }
	       //设置身份证正面
	       if(user['icardImg1'] != "" && user['icardImg1'] != null){
	           $("input[name=icardImg1]").val(user['icardImg1']);
	           $('#file_upload_icard1').clearFileId();
	           $('#file_upload_icard1').setFileId(user['icardImg1'],true,true);
	       }else{
	    	   $('#file_upload_icard1').clearFileId();
	       }
	       //设置身份证反面
	       if(user['icardImg2'] != "" && user['icardImg2'] != null){
	           $("input[name=icardImg2]").val(user['icardImg2']);
	           $('#file_upload_icard2').clearFileId();
	           $('#file_upload_icard2').setFileId(user['icardImg2'],true,true);
	       }else{
	    	   $('#file_upload_icard2').clearFileId();
	       }
	       //设置学生证照片一
	       if(user['sicardImg1'] != "" &&　user['sicardImg1']　!= null){
	           $("input[name=sicardImg1]").val(user['sicardImg1']);
	           $('#file_upload_scard1').clearFileId();
	           $('#file_upload_scard1').setFileId(user['sicardImg1'],true,true);
	       }else{
	    	   $('#file_upload_scard1').clearFileId();
	       }
	       //设置学生证照片二
	       if(user['sicardImg2'] != "" &&　user['sicardImg2']　!= null){
	           $("input[name=sicardImg2]").val(user['sicardImg2']);
	           $('#file_upload_scard2').clearFileId();
	           $('#file_upload_scard2').setFileId(user['sicardImg2'],true,true);
	       }else{
	    	   $('#file_upload_scard2').clearFileId();
	       }
	       //设置学籍档案电子版照片
	       if(user['sicardImg3'] != "" &&　user['sicardImg3']　!= null){
	           $("input[name=sicardImg3]").val(user['sicardImg3']);
	           $('#file_upload_scard3').clearFileId();
	           $('#file_upload_scard3').setFileId(user['sicardImg3'],true,true);
	       }else{
	    	   $('#file_upload_scard3').clearFileId();
	       }
	  }
	   
	   //填充合同信息
	  function setContractData(data){
		   var con =data['contract'];
		   var shop = data['shop'];
		   $("#borrowF input[type!=button]").each(function(){
	           var name = $(this).attr('name');
	           var value = con[name];
	           if(value != null && value != undefined){
	        	   $(this).val(value);
	           }
	       });
		   $("input[name=borrowTime]").val(con['createTime']);
		   $("#borrowF select").each(function(){
			   var name = $(this).attr('name');
			   var value = con[name];
			   if(value != null && value != undefined){
	        	   $(this).val(value);
			   }
	       });
		   $("select[name=shopNum]").val(shop['number']);
	       $("select[name=shopName]").val(shop['name']);
	       $("input[name=shopId]").val($("select[name=shopNum] option:selected").attr("id"));
	       //设置图片信息
	       //设置通话记录
	       if(con['callListImg'] != "" && con['callListImg'] != null){
	           $("input[name=callListImg]").val(con['callListImg']);
	           $('#file_upload_call').clearFileId();
	           $('#file_upload_call').setFileId(con['callListImg'],true,true);
	       }else{
	    	   $('#file_upload_call').clearFileId();
	       }
	       //银行卡照片
	       if(con['bankImg'] != "" && con['bankImg'] != null){
	           $("input[name=bankImg]").val(con['bankImg']);
	           $('#file_upload_bank').clearFileId();
	           $('#file_upload_bank').setFileId(con['bankImg'],true,true);
	       }else{
	    	   $('#file_upload_bank').clearFileId();
	       }
	       //客户现场照
	       if(con['headImg'] != "" && con['headImg'] != null){
	           $("input[name=headImg]").val(con['headImg']);
	           $('#file_upload').clearFileId();
	           $('#file_upload').setFileId(con['headImg'],true,true);
	       }else{
	    	   $('#file_upload').clearFileId();
	       }
	   }
	   
	   //预览合同
	   function previewContract(){
	       var id = $("input[name=id]").val();
	       if(id == ""){
	    	   alert("请先提交合同");
	    	   return false;
	       }
	       window.open(path+"/preview.do?id="+id);
	   }
	   
	   //保存合同信息
	   function saveContract(){
		var payMonth = $("input[name=payMonth]").val();
	    if(payMonth == '自动生成' || isNaN(payMonth)==true){
		     $("input[name=payMonth]").val('');
		}
	    var re = formValidate("borrowF");
        if(re==false){
            return;
        }
	    var type = $("input[name=type]:checked").val();
	     var msg = "";
	     //修改月还款额的值
	     //如果是学生的
	      if(type == '0'){
	         msg = "当前是学生版本，确认提交么？";
	         $("input[name=rank]").val('0');
	      }else if(type == '1'){
	         var point = calPoint();
	         $("input[name=rank]").val(point);
	         msg = "当前是社会版，信息评分为" + point + "分，确认提交么？";
	      }
	      if(confirm(msg)){
	         var query = $("#borrowF").formSerialize();
	         var cardId = $("input[name=cardId]").val();
	         query+="&cardId="+cardId;
	         request(path+"/addContract.do",query,function(text){
	              var obj = eval('(' + text + ')');
	              if(obj.code != 1){
                  alert("提交失败");
                  return ;
                 }else{
                    alert("提交成功");
                    var data = obj.data;
                    $("input[name=contractId]").val(data.contractId);
                    $("input[name=borrowTime]").val(data.createTime);
                    $("input[name=payMonth]").val(data.payMonth);
                    $("input[name=id]").val(data.id);
                    $("select[name=payDay]").val(data.payDay);
                    if(isIn==true){
                    	request(path+"/appordersN.do","",selectF);
                    }
                 }
	         });
	        
	      }
	   }
	   
	  //保存联系人处理函数
	  var contactF = function(text){
	      var obj = eval('(' + text + ')');
          if(obj.code != 1){
              alert("保存失败");
              return ;
         }else{
             alert("保存成功");
             var row = $("#contactT").find("tr.data").eq(contactIndex);
             row.find("input[name=idC]").val(obj.data);
         }
	  };
      
      function onUploadSuccessHeadimg(e) {
          $("input[name=headImg]").val($("#file_upload").getFileId());
      }
      function onUploadSuccessbankImg(e) {
          $("input[name=bankImg]").val($("#file_upload_bank").getFileId());
      }
      function onUploadSuccesscallListImg(e) {
          $("input[name=callListImg]").val($("#file_upload_call").getFileId());
      }
      function onUploadSuccessicardImg1(e) {
          $("input[name=icardImg1]").val($("#file_upload_icard1").getFileId());
      }
      function onUploadSuccessicardImg2(e) {
          $("input[name=icardImg2]").val($("#file_upload_icard2").getFileId());
      }
      function onUploadSuccesssicardImg1(e) {
          $("input[name=sicardImg1]").val($("#file_upload_scard1").getFileId());
      }
      function onUploadSuccesssicardImg2(e) {
          $("input[name=sicardImg2]").val($("#file_upload_scard2").getFileId());
      }
      function onUploadSuccesssicardImg3(e) {
          $("input[name=sicardImg3]").val($("#file_upload_scard3").getFileId());
      }